﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

using MINERP.DAL;
using MINERP.DAL.InfoConfig;
using MINERP.COM;

namespace MINERP.BLL.InfoConfig
{
    public  class DelegateBLL
    {
        #region //Private Fields


        #endregion


        #region //Public Properties

        #endregion



        #region //Public Methods
        /// <summary>
        /// Get shop list
        /// </summary>
        /// <returns></returns>
        public List<OutProcessShop> GetOutProcessShopList()
        {
            //return DelegateDAL.GetOutProcessShopList();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.OutProcessShop.ToList();
            }
        }

        /// <summary>
        /// Get shop object
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OutProcessShop GetOutProcessShopObj(int id)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.OutProcessShop.FirstOrDefault(o => o.OutShopId == id);
            }
            //return DelegateDAL.GetOutProcessShopObj(id);
        }
        public Tb_OutShop_ProductCategory GetOutShopProductCategory(int productId,int outShopId)
        {
            return DelegateDAL.GetOutShopProductCategory(productId,outShopId);
        }
        /// <summary>
        /// add OutProcessShop
        /// </summary>
        /// <param name="obj"></param>
        public void Add(OutProcessShop obj)
        {
            DelegateDAL.Add(obj);
        }
        /// <summary>
        /// update OutProcessShop
        /// </summary>
        public void Update()
        {
            DelegateDAL.Update();
        }
        /// <summary>
        /// Delete OutProcessShop
        /// </summary>
        /// <param name="obj"></param>
        public void Delete(OutProcessShop obj)
        {
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {

                OutProcessShop outProcessShop = entity.OutProcessShop.First(o=>o.OutShopId ==obj.OutShopId);
                entity.DeleteObject(outProcessShop);
                entity.SaveChanges();

            }
            //DelegateDAL.Delete(obj);
        }
        #endregion

        public OutProcessShop GetOutProcessShopObj(string p)
        {
            return DelegateDAL.GetOutProcessShopObj(p);
        }

        public bool ProductHasDefaultOutShop(int productId,out OutProcessShop Outshop)
        {
            Outshop = null;
            bool flag = false; ;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                List<Tb_OutShop_ProductCategory> list = entities.Tb_OutShop_ProductCategory.Where(op => op.ProductId ==productId).ToList();
                foreach (var item in list)
                {
                    if (item.IsDefaultOutShop)
                    {
                        flag = true;
                        Outshop = entities.OutProcessShop.FirstOrDefault(s => s.OutShopId == item.OutShopId);
                    }
                }
            }

            return flag;
        }

        public void ChangeDefaultOutShop(int productId,int outShopId)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                Tb_OutShop_ProductCategory op = entities.Tb_OutShop_ProductCategory.FirstOrDefault(o=>o.ProductId ==productId&&o.OutShopId ==outShopId);
                op.IsDefaultOutShop = false;
                entities.SaveChanges();
            }
        }

        public void ProductActiveCode(int outShopId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessShop Outshop = db.OutProcessShop.FirstOrDefault(s => s.OutShopId == outShopId);
                Outshop.SHACODE = MD5HashCode.GetHash(outShopId.ToString() + DateTime.Now.ToString());
                db.SaveChanges();
            }
        }

    }
}
